package com.mooc;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.baidu.cloud.media.player.IMediaPlayer;
import com.baidu.cloud.videoplayer.bar.SimpleMediaController;
import com.baidu.cloud.videoplayer.widget.BDCloudVideoView;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by zirconi on 2017/6/30.
 */

public class BDPlayerViewWithControllor extends LinearLayout implements BDCloudVideoView.OnPlayerStateListener,
        IMediaPlayer.OnBufferingUpdateListener {

    BDCloudVideoView mBDPlayerView;
    RelativeLayout mViewHolder;
    SimpleMediaController mediaController;
    View mRoot;
    private Timer barTimer;

    public BDPlayerViewWithControllor(Context context) {
        super(context);
        mBDPlayerView = new BDCloudVideoView(context);

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View mRoot = inflater.inflate(R.layout.activity_simple_video_playing, null);
        mRoot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickEmptyArea(v);
            }
        });
        mediaController = (SimpleMediaController) mRoot.findViewById(R.id.media_controller_bar);
        mViewHolder = (RelativeLayout) mRoot.findViewById(R.id.view_holder);
        RelativeLayout.LayoutParams rllp = new RelativeLayout.LayoutParams(-1, -1);
        rllp.addRule(RelativeLayout.CENTER_IN_PARENT);
        mViewHolder.addView(mBDPlayerView, rllp);
        mediaController.setMediaPlayerControl(mBDPlayerView);

        mBDPlayerView.setOnBufferingUpdateListener(this);
        mBDPlayerView.setOnPlayerStateListener(this);
        addView(mRoot);
    }

    public BDCloudVideoView getBDPlayerView() {
        return mBDPlayerView;
    }

    public void onClickEmptyArea(View v) {
        if (barTimer != null) {
            barTimer.cancel();
            barTimer = null;
        }
        if (this.mediaController != null) {
            if (mediaController.getVisibility() == View.VISIBLE) {
                mediaController.hide();
            } else {
                mediaController.show();
                hideOuterAfterFiveSeconds();
            }
        }
    }

    private void hideOuterAfterFiveSeconds() {
        if (barTimer != null) {
            barTimer.cancel();
            barTimer = null;
        }
        barTimer = new Timer();
        barTimer.schedule(new TimerTask() {

            @Override
            public void run() {
                if (mediaController != null) {
                    mediaController.getMainThreadHandler().post(new Runnable() {

                        @Override
                        public void run() {
                            mediaController.hide();
                        }

                    });
                }
            }

        }, 5 * 1000);
    }

    @Override
    public void onBufferingUpdate(IMediaPlayer mp, int percent) {
        if (mediaController != null && mBDPlayerView != null) {
            mediaController.onTotalCacheUpdate(percent * mBDPlayerView.getDuration() / 100);
        }
    }

    @Override
    public void onPlayerStateChanged(BDCloudVideoView.PlayerState nowState) {
        if (mediaController != null) {
            mediaController.changeState();
        }
    }

}
